﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;

using ECCIT;
using ECCIT.Data;
using ECCIT.Business.DAL;

using OgilvyOne.UKNow.Globalization;
using OgilvyOne.UKNow.Utility;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.IDAL;

namespace OgilvyOne.UKNow.DAL
{
    public class ArtFormDAL : UKNowBaseDAL, IDALArtForm
    {
        private const string TBL_NAME           = "UKNOW_ART_FORM";
        private const string COL_LANG_ID        = "COL_LANG_ID";
        private const string COL_FORM_ID        = "COL_FORM_ID";
        private const string COL_FORM_NAME      = "COL_FORM_NAME";
        private const string COL_FORM_DESC      = "COL_FORM_DESC";
        private const string COL_FORM_DETAIL    = "COL_FORM_DETAIL";
        private const string COL_FORM_ORDER     = "COL_FORM_ORDER";

        public override string TableName { get { return TBL_NAME; } }

        public override Type ModelType { get { return typeof(ArtFormModel); } }

        [PrimaryKey]
        public IField LanguageID { get { return FieldFactory.Create(ModelType, "LanguageID", ColumnFactory.CreateColumn(COL_LANG_ID, DataType.Int)); } }
        [PrimaryKey]
        public IField ID { get { return FieldFactory.Create(ModelType, "ID", ColumnFactory.CreateColumn(COL_FORM_ID, DataType.Int)); } }
        public IField Name { get { return FieldFactory.Create(ModelType, "Name", ColumnFactory.CreateColumn(COL_FORM_NAME, DataType.String, 50)); } }
        public IField ShortDescription { get { return FieldFactory.Create(ModelType, "ShortDescription", ColumnFactory.CreateColumn(COL_FORM_DESC, DataType.String, 500)); } }
        public IField Detail { get { return FieldFactory.Create(ModelType, "Detail", ColumnFactory.CreateColumn(COL_FORM_DETAIL, DataType.NText)); } }
        public IField Order { get { return FieldFactory.Create(ModelType, "Order", ColumnFactory.CreateColumn(COL_FORM_ORDER, DataType.Int)); } }

        public override IField[] InsertFields { get { return new IField[] { LanguageID, ID, Name, ShortDescription, Detail, Order }; } }
        public override IField[] SelectFields { get { return InsertFields; } }
        public override IField[] UpdateFields { get { return InsertFields; } }

        public ArtFormDAL() { }

        public ArtFormDAL(LanguageType languageType) { this.Language = LanguageFactory.Create(languageType); }

        #region IDALArtForm Members

        public ArtFormModel GetArtFormModelByID(int? id)
        {
            ArtFormModel model = (ArtFormModel)GetModelByPrimaryKey(typeof(ArtFormDAL), new KeyValuePair<IField, object>[] { new KeyValuePair<IField, object>(LanguageID, (int)this.Language.LanguageType), new KeyValuePair<IField, object>(this.ID, id) });
            return model;
        }

        public ResultInfo Save(ArtFormModel modelInfo)
        {
            if (modelInfo == null) throw new ArgumentNullException("modelInfo");

            ResultInfo result = new ResultInfo(false);

            try
            {
                int rowsAffected = 0;
                if (modelInfo.ID == null)   // insert
                {
                    modelInfo.ID = SqlUtil.GetMaxRecordID(TableName, COL_FORM_ID, COL_LANG_ID, (int)this.Language.LanguageType);
                    rowsAffected = Insert(modelInfo, null);
                }
                else // update
                {
                    rowsAffected = UpdateByPrimaryKey(modelInfo, null);
                }

                if (rowsAffected > 0)
                {
                    result.Message = "艺术形式数据保存成功！";
                    result.Result = true;
                    result.ReturnObject = modelInfo;
                }
            }
            catch (Exception ex)
            {
                result.Exception = ex;
                result.Message = string.Format("保存艺术形式数据时发生异常。异常信息：{0}", ex.Message);
            }

            return result;
        }

        public IList<ArtFormModel> GetArtFormModels()
        {
            string whereClause = string.Format("where {0}={1}", COL_LANG_ID, (int)this.Language.LanguageType);
            return GetModels(whereClause, PagingParameterFactory.CreateNonPagingParameter(SortingParameterFactory.CreateInstance(Order.PropertyName, SortOrder.Asc))).List.Cast<ArtFormModel>().ToList();
        }

        public ResultInfo Delete(IList<int?> idList)
        {
            ResultInfo result = new ResultInfo(false);
            using (IDbConnection conn = DBHelper.GetDbConnection())
            {
                conn.Open();
                IDbTransaction trans = conn.BeginTransaction();
                try
                {
                    foreach (int? id in idList)
                        DeleteModelByPrimaryKey(trans, this.GetType(), new KeyValuePair<IField, object>[] { new KeyValuePair<IField, object>(LanguageID, (int)this.Language.LanguageType), new KeyValuePair<IField, object>(this.ID, id) });

                    trans.Commit();
                    result.Result = true;
                    result.Message = "艺术形式数据删除成功！";
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    result.Message = string.Format("删除艺术形式数据时发生异常。异常信息：{0}", ex.Message);
                }
                finally
                {
                    trans.Dispose();
                }
            }

            return result;
        }

        #endregion
    }
}
